package com.axonactive.yolo.notification;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.apache.log4j.Logger;

import com.twilio.sdk.TwilioRestClient;
import com.twilio.sdk.TwilioRestException;
import com.twilio.sdk.resource.factory.MessageFactory;
import com.twilio.sdk.resource.instance.Message;

public class TwilioSMSService implements SMSService {

	private static final Logger LOGGER=Logger.getLogger(TwilioSMSService.class);
	// Find your Account Sid and Token at twilio.com/user/account
	public static final String ACCOUNT_SID = "AC323c8cca16f83dc2f69f16c09956a0ce";
	public static final String AUTH_TOKEN = "e47cc0d3585005b244f22551d4245186";
	private static Message message=null;
	private static final String TO_TRINH="+84935910309";
	private static final String FROM= "+14194649783";

	@Override
	public boolean sendMessage(String from, String to, String body, String mediaUrl) {
		TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN);

		// Build a filter for the MessageList
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("Body",
				body));
		params.add(new BasicNameValuePair("To", to));
		params.add(new BasicNameValuePair("From", from));
//		params.add(new BasicNameValuePair("MediaUrl", mediaUrl));
		MessageFactory messageFactory = client.getAccount().getMessageFactory();
		boolean res=false;

		try {
			message = messageFactory.create(params);
			res=true;
		} catch (TwilioRestException e) {
			LOGGER.info("Send Message problem",e);
		}
		return res;
	}

	@Override
	public boolean sendMessage(String from, String to, String body) {
		return sendMessage(from, to, body,"");
	}

	@Override
	public Object getLastStatus() {
		return message;
	}

	@Override
	public boolean sendMessage(String to, String body) {
		LOGGER.info("send to "+to+" body: "+body + "length: "+body.length());
//		return sendMessage(FROM, to, body,"");
		return sendMessage(FROM, TO_TRINH, body);
	}

	public static void main(String[] arg){
		new TwilioSMSService().sendMessage(FROM, TO_TRINH, "abc");
	}
}
